package com.any0ne.loadDemo.controller;

import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
public class IndexController {
    @RequestMapping("/index")
    public String index(){
        return "Hello 4ut15m";
    }
    /**
     * 测试ProcessBuilder的hook，可以在加好agent之后访问
     * curl http://localhost:8080/exec?cmd=whoami
     * 控制台中可以看到hook结果.
     * */
    @RequestMapping("/exec")
    public String CommandExec(HttpServletRequest request, HttpServletResponse response){
        String res = "Parameter cmd can not be empty.";
        String cmd = request.getParameter("cmd");
        try {
            res = IOUtils.toString(new ProcessBuilder(cmd).start().getInputStream(),"utf-8");
        } catch (IOException e) {
            e.printStackTrace();
        }

        return res;
    }
}
